package net.esj.basic.plugins.spring.factory.xml.parser;

import java.util.HashMap;
import java.util.Map;

import net.esj.basic.dao.hibernate.Hibernate3SessionLocale;
import net.esj.basic.dao.hibernate.Hibernate4SessionLocale;
import net.esj.basic.dao.hibernate.HibernateSessionLocale;

import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

@Deprecated
public class HibernateSessionLocaleBeanDefinitionParser extends
		AbstractSingleBeanDefinitionParser {

	public static Class DEFAULT_HIBERNATE_SESSION = Hibernate4SessionLocale.class;
	
	public static Map<String,Class> HIBERNATE_BEAN_MAP;
	static{
		HIBERNATE_BEAN_MAP = new HashMap<String, Class>();
		HIBERNATE_BEAN_MAP.put("hibernate3", Hibernate3SessionLocale.class);
		HIBERNATE_BEAN_MAP.put("hibernate4", Hibernate4SessionLocale.class);
	}
	@Override
	protected void doParse(Element element, ParserContext parserContext,
			BeanDefinitionBuilder builder) {
		if (!parserContext.getRegistry().containsBeanDefinition(getBeanClassName(element))) {
			String sessionFactory = element.getAttribute("sessionFactory");
			if (!StringUtils.hasText(sessionFactory)) {
				sessionFactory = "sessionFactory";
			}
			builder.addPropertyReference("sessionFactory", sessionFactory);
			parserContext.registerBeanComponent(
					new BeanComponentDefinition(builder.getBeanDefinition(),getBeanClassName(element)));
		}
	}
	@Override
	protected String getBeanClassName(Element element) {
		String clazzType = element.getAttribute("type");
		Class hsl = HIBERNATE_BEAN_MAP.get(clazzType);
		if(hsl==null){
			hsl = DEFAULT_HIBERNATE_SESSION;
		}
		return hsl.getName();
	}
	
	
}
